<template>
  <div class="login">
    <div style="text-align: center; font-size: 40px; margin-top: 80px;color: black;vertical-align:bottom; ">
      <img :src="logo"
           style="width: 100px;display:inline-block;bottom: 10px">
      <span style="font-size: 90px">鹿行集团有限公司</span>
    </div>
    <div style="color: black;text-align: center; font-size: 50px">
      <span>Deer Walk Group Company Limited</span>
    </div>
    <div>
      <el-card
          style="width: 650px;height: 400px;margin:50px auto;background-color: rgba(255,255,255,0.3);">
        <h1 style="text-align: center; margin: 30px auto">鹿行企业用户登录</h1>
        <el-form style="padding: 20px" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px"
                 class="demo-ruleForm">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="ruleForm.password" placeholder="请输入密码"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">登录
            </el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
          <el-form style="text-align: center;padding: 10px">
            <el-checkbox v-model="radio" label="1">记住我</el-checkbox>
            <el-checkbox v-model="checked">
              已经阅读并同意
              <el-link type="primary" href="http://localhost:9000/Protocol" target="_blank">
                相关协议
              </el-link>
            </el-checkbox>
          </el-form>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      logo:require('@/assets/imgs/deer.png'),
      ruleForm: {
        username: '',
        password: ''
      },
      radio: true,
      checked: true,
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 4, max: 10, message: '长度在 4 到 10 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 4, max: 10, message: '长度在 4 到 10 个字符', trigger: 'blur'}
        ]
      },
    };
  },
  methods: {
    submitForm: function (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9090/admins/login';
          console.log('url = ' + url);
          let fromData = this.qs.stringify(this.ruleForm);
          console.log('fromData=' + fromData)
          this.axios.post(url, fromData).then((response) => {
                let responseBody = response.data;
                console.log(responseBody);
                if (responseBody.state == 20000) {
                  this.$message({
                    showClose: true,
                    message: '登录成功',
                    type: 'success'
                  });
                  //接收服务器端响应的jwt
                  let jwt = responseBody.data;
                  console.log('jwt=' + jwt);
                  //将服务器端响应过来的jwt存入localstorage中
                  localStorage.setItem('jwt', jwt);
                  localStorage.setItem('localRuleForm', JSON.stringify(this.ruleForm));
                  this.$router.push('/sys-admin/index');
                } else {
                  this.$message({
                    showClose: true,
                    message: responseBody.message,
                    type: 'error'
                  });
                }
              }
          );
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    loadLocalUserName() {
      let LocalRuleFormString = localStorage.getItem('LocalRuleForm');
      let LocalRuleForm = JSON.parse(LocalRuleFormString);
      if (LocalRuleForm.username) {
        this.ruleForm = LocalRuleForm;
      }
    }
  },
  mounted() {
    this.loadLocalUserName();
  }
}
</script>
<style>

div > img {
  vertical-align: bottom;
}

</style>